#!/bin/bash

# 检查root权限
[ "$(id -u)" -ne 0 ] && { echo "请使用root权限运行"; exit 1; }

# 选择操作系统
echo "该脚本同时支持一键部署如下系统的软件源："
echo "1) Rocky Linux 9"
echo "2) Ubuntu 24.04"
echo "----------------------"
source /etc/os-release
[ "${ID}" == "rocky" ] && os="1"
[ "${ID}" == "ubuntu" ] && os="2"
case $os in
1)
    # Rocky Linux 9配置
    echo "当前操作系统是: Rocky Linux 9，选择镜像源:"
    echo "1) 阿里云"
    echo "2) 上海交大"
    echo "3) 南京大学"
    read -p "输入选择(1/2/3): " mirror

    [ "$mirror" -eq 1 ] && url="mirrors.aliyun.com/rockylinux"
    [ "$mirror" -eq 2 ] && url="mirrors.sjtug.sjtu.edu.cn/rocky"
    [ "$mirror" -eq 3 ] && url="mirror.nju.edu.cn/rocky"
    [ -z "$url" ] && { echo "无效选择"; exit 1; }

    # 备份并配置源
    mkdir -p /etc/yum.repos.d/backup
    mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
    cat > /etc/yum.repos.d/rocky.repo << EOF
	
[AppStream]
name=Rocky Linux $releasever - AppStream
baseurl=https://mirrors.aliyun.com/rockylinux/9/AppStream/x86_64/os/
enabled=1

[BaseOS]
name=Rocky Linux $releasever - BaseOS
baseurl=https://mirrors.aliyun.com/rockylinux/9/BaseOS/x86_64/os/
enabled=1

[extras]
name=Rocky Linux $releasever - extras
baseurl=https://mirrors.aliyun.com/rockylinux/9/extras/x86_64/os/
enabled=1

EOF
  ;;

2)
    # Ubuntu 24.04配置
    echo "当前操作系统是: Ubuntu 24.04，选择镜像源:"
    echo "1) 阿里云"
    echo "2) 清华源"
    echo "3) 南京大学"
    read -p "输入选择(1/2/3): " mirror
    read -p "是否需要src源(yes/no): " srctype
    [ "$mirror" -eq 1 ] && url="mirrors.aliyun.com"
    [ "$mirror" -eq 2 ] && url="mirrors.tuna.tsinghua.edu.cn"
    [ "$mirror" -eq 3 ] && url="mirror.nju.edu.cn"
    [ -z "$url" ] && { echo "无效选择"; exit 1; }

    # 备份并配置源
    cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak
    rm -rf /etc/apt/sources.list.d/*
    cat > /etc/apt/sources.list << EOF

deb https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse


EOF
    apt-get update
    ;;
*)
    echo "无效选择"
    exit 1
    ;;
esac

echo "配置完成!"
